import {
    http,
    httpFile,
    test,
} from '@/utils/request/index.js';

import {
    getLoginInfo,
    getShareId
} from '@/lib/storage';

import {
    baseShopId,
} from '@/utils/request/baseConfig';

import {
    setHasNetwork,
    getHasNetwork
} from "@/lib/storage";

const shopId = baseShopId;


/**
 * 测试接口
 * @param { String } business // apiName
 * @param { Object } data // proprsData
 */
export const testHttp = (business, data = {}) => {
    const customerId = getLoginInfo().customerId || '';
    const defData = {
        showLoading: true,
        hideLoading: true,
        shopId,
        customerId: customerId
    }
    data = Object.assign({}, defData, data);
    const baseData = {
        showLoading: data.showLoading,
        hideLoading: data.hideLoading,
        currentPage: data.currentPage || 1,
        pageSize: data.pageSize || 10,
        data
    }
    return test.post(business, baseData);
}

let hasNetwork = true;
const getNetWorkStatus = () => {
    uni.onNetworkStatusChange(res => {
        console.log('res', res);
        hasNetwork = res.isConnected;
    });
}

getNetWorkStatus();

/**
 * 正式接口
 * @param { String } business // apiNumber
 * @param { Object } data // proprsData
 */
export const baseHttp = (business, data = {}, options = {} ) => {

    if (!hasNetwork) {

        console.log('无网络~');
        getHasNetwork() && uni.showModal({
            title: '提示',
            content: '网络出现问题，请检查您的网络',
            showCancel: false,
            success: res => {
                if (res.confirm) {
                    setHasNetwork(true)
                }
            },
            complete: () => {
                uni.stopPullDownRefresh();
            }
        })
        setHasNetwork(false)
        return;
    }
    const customerId = getLoginInfo().customerId || '';
    const shareId = getShareId() || '';
    // console.log('customerId', customerId);
    //console.log('shareId', shareId);
    /*  */
    let baseData;
    if(!data.otherReqData){
        const defData = {
            showLoading: true,
            hideLoading: true,
            shopId,
            distributorShareId: shareId,
            customerId: customerId
        }
        data = Object.assign({}, defData, data);
        baseData = {
            showLoading: data.showLoading,
            hideLoading: data.hideLoading,
            currentPage: data.currentPage || 1,
            pageSize: data.pageSize || 10,
            data,
        }
    }else{
        delete data["otherReqData"];
        baseData = Object.assign({
            currentPage: data.currentPage || 1,
            pageSize: data.pageSize || 10,
        },data);
    }
    return http.post(business, baseData, options);
}

/**
 * 正式接口 文件上传
 * @param { String } business // apiNumber
 * @param { Object } data // proprsData
 */
export const baseFileHttp = (business, data = {}) => {
    const customerId = getLoginInfo().customerId || '';
    console.log('customerId', customerId);

    const defData = {
        showLoading: true,
        hideLoading: true
    };
    data = Object.assign({}, defData, data);

    const baseData = {
        filePath: data.filePath,
        name: 'file',
        header: {
            'content-type': 'multipart/form-data'
        },
        formData: {
            showLoading: data.showLoading,
            hideLoading: data.hideLoading

        }
    }
    // httpFile.setConfig((config) => {
    //     /* config 为默认全局配置*/
    //     // config.baseUrl = 'https://fy.csgxcf.com/FileSystem/'; /* 根域名 */
    //     // config.header = {
    //     //     'content-type': 'multipart/form-data'
    //     // };
    //     return config
    // })
    return httpFile.upload(business, baseData);
}